package com.ukao.pad.dialog;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.ukao.pad.R;
import com.ukao.pad.base.BaseDialogFragment;
import com.ukao.pad.bean.AccountLoginBean;
import com.ukao.pad.bean.TokenBean;
import com.ukao.pad.consts.Constant;
import com.ukao.pad.consts.SaveParamets;
import com.ukao.pad.retrofit.ApiCallback;
import com.ukao.pad.utils.CheckUtils;
import com.ukao.pad.utils.CipherUtils;
import com.ukao.pad.utils.T;
import com.ukao.pad.utils.Utils;
import com.ukao.pad.widget.ClearEditText;
import com.ukao.pad.widget.StateButton;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 账号登录对话框
 */
public class AccountDialogFragment extends BaseDialogFragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    public static final int LOGIN_DIALOG_DESTORT = 1;
    public static final int LOGIN_SUCCEED = 0;

    Unbinder unbinder;
    @BindView(R.id.tv_title)
    TextView title;
    @BindView(R.id.account_edit_text)
    ClearEditText accountEditText;
    @BindView(R.id.pass_edit_text)
    ClearEditText passEditText;
    @BindView(R.id.login_btn)
    StateButton loginBtn;
    @BindView(R.id.progressBar2)
    ProgressBar progressBar2;
    @BindView(R.id.login_progress_layout)
    LinearLayout loginProgressLayout;

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private String strAccount;
    private String strPass;


    public static AccountDialogFragment newInstance(String param1, String param2) {
        AccountDialogFragment fragment = new AccountDialogFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_account_dialog, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getDialog().setCanceledOnTouchOutside(false);
        accountEditText.setText(SaveParamets.getAccount());

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mOnConfirmClickListener!=null){
            mOnConfirmClickListener.onChildItemClick(LOGIN_DIALOG_DESTORT,null);
        }
        unbinder.unbind();
    }

    @OnClick({R.id.login_btn,R.id.close_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.login_btn:
                    strAccount=getText(accountEditText);
                     strPass=getText(passEditText);
                if (CheckUtils.isEmpty(strAccount)){

                    T.show(getString(R.string.please_enter_your_account));
                    return;
                }else  if (CheckUtils.isEmpty(strPass)){
                    T.show(getString(R.string.please_enter_your_password));
                    return;
                }

                appSecretData();
                break;
            case   R.id.close_btn:
                dismiss();
                break;


        }
    }


    public void appSecretData(     ) {
        showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        addSubscription(apiStores().getToken(Constant.firstParameter(mHashMap)),
                new ApiCallback<TokenBean>() {
                    @Override
                    public void onSuccess(TokenBean model) {

                        if (model.getHttpCode() == 200) {
                            SaveParamets.saveloginSign(activity, model.getData().getAppSecret());
                            loginRequest();
                        }else{
                            hideLoading();
                            T.show(model.getMsg());
                        }

                    }

                    @Override
                    public void onNoNewWork() {
                        hideLoading();
                    }


                    @Override
                    public void onFinish() {



                    }


                });
    }

    private void showLoading() {
        loginBtn.setText("");
        loginProgressLayout.setVisibility(View.VISIBLE);

    }

    private void hideLoading() {
        loginBtn.setText("登录");
        loginProgressLayout.setVisibility(View.GONE);
    }

    public void loginRequest() {   /*13708007874本地   //xxdlggc@xxd   */  /**/
        String sign = SaveParamets.getsaveloginSign(Utils.getContext());
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("account",strAccount);
        mHashMap.put("password", CipherUtils.md5(CipherUtils.md5(strPass) + sign));
        addSubscription(apiStores().login(Constant.createParameter(mHashMap)),
                new ApiCallback<AccountLoginBean>() {
                    @Override
                    public void onSuccess(AccountLoginBean model) {

                        if (model.getHttpCode() == 200) {
                            AccountLoginBean mitem = model.getData();
                            if (mOnConfirmClickListener!=null){
                                SaveParamets.setAccount(strAccount);
                                mOnConfirmClickListener.onChildItemClick(LOGIN_SUCCEED,mitem);
                            }

                        } else {
                           T.show(model.getMsg());
                        }
                    }

                    @Override
                    public void onNoNewWork() {

                    }

                    @Override
                    public void onFinish() {

                       hideLoading();
                    }


                });
    }

}
